package com.acme.bankapp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * This class represents client which automatically send request to {@link com.acme.bankapp.service.bank.network.BankServer} object
 *
 * @author Alexander Kotov (kotov.alex.22@gmail.com)
 */
public class AutoBankClient {
    private final static int PORT = 12345;
    private final static String LOCAL_HOST = "127.0.0.1";
    private final ScheduledExecutorService requestSenderScheduler = Executors.newSingleThreadScheduledExecutor();

    private void sendMessage(final PrintWriter out, final String message) {
        out.println(message);
        System.out.println("client ==> " + "'" + message + "'");
    }

    /**
     * This method starts client
     */
    public void run() {
        try {
            Socket socket = new Socket(LOCAL_HOST, PORT);
            PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
            BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            System.out.println("Connected to " + LOCAL_HOST + " in port " + PORT);
            String salutation = input.readLine();
            System.out.println("'" + salutation + "' <== salutation from server");

            RequestSender requestSender = new RequestSender(input, output);

            requestSenderScheduler.scheduleAtFixedRate(requestSender, 0, 50, TimeUnit.MILLISECONDS);

        } catch (UnknownHostException unknownHostException) {
            unknownHostException.printStackTrace();
            System.err.println("Connection to unknown host failed");
        } catch (IOException ioException) {
            ioException.printStackTrace();
            System.err.println(ioException.getMessage());
        } catch (NullPointerException nullPointerException) {
            nullPointerException.printStackTrace();
            System.err.println(nullPointerException.getMessage());
        }
    }

    private class RequestSender implements Runnable {
        private final BufferedReader in;
        private final PrintWriter out;
        private int currentId = 100;

        public RequestSender(final BufferedReader in, final PrintWriter out) {
            this.in = in;
            this.out = out;
        }

        @Override
        public void run() {
            try {
                String message = String.format("add id=%d;accounttype=s;balance=100;overdraft=50;name=test%d;gender=m;email=test@mail.com;", currentId, currentId++);
                sendMessage(out, message);
                String reply = in.readLine();
                System.out.println("'" + reply + "' <== server");
            } catch (IOException ioException) {
                ioException.getStackTrace();
                System.err.println(ioException.getMessage());
            }
        }
    }

}
